package com.zhaotianhao.springboot_demo_fire.controller;


import com.zhaotianhao.springboot_demo_fire.entity.Types;
import com.zhaotianhao.springboot_demo_fire.service.ITypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Z-7
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/types")
public class TypesController {
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private ITypesService iTypesService;
    @PostMapping("list")
    public List<Types> getTypeList(){
        List<Types> types = (List<Types>) redisTemplate.opsForValue().get("types");
        if(CollectionUtils.isEmpty(types)){
            types = iTypesService.list();
            redisTemplate.opsForValue().set("types",types);
        }
        return types;
    }
}
